/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/* 
 * Copyright (c) 2009 City University of Hong Kong
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License m_version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Author: Wilson Thong (wilsonwk@ee.cityu.edu.hk) 
 */
#ifndef BMSN_TOPOLOGY_H
#define BMSN_TOPOLOGY_H

#include "jitter-sim-include.h"
#include "base-topology.h"
#include <string>

namespace ns3 {
namespace eecityu {

using namespace std;

Ipv4Address GetIpv4AddressFromNetDevice (const Ptr<NetDevice const> dev);
uint32_t GetIpv4AddressAtOctet (const Ipv4Address& addr, uint32_t i);

/**
 * \brief Contains the nodes and net devices used to build a Bidirection Manhattan
 * street network (BMSN).
 *
 * \see BmsnGenerator
 */
class BmsnTopology : public BaseTopology
{
public:
  //static TypeId GetTypeId (void);
  /**
   * \brief Construct a BmsnTopology object to contain the nodes and net devices
   */
  BmsnTopology(void);
  /**
   * \brief Set the nodes and net devices and check if this is really
   * a BMSN topology
   */
  void SetNodesAndNetDevices (
    const NodeContainer& nodes, 
    const NetDeviceContainer& devs);
  /**
   * \brief Get the number of rows of the BMSN topology
   *
   * \return The number of rows of this BMSN topology
   */
  uint32_t GetNRows () const;

private:
  /**
   * \return True if this object does not contain any nodes nor net devices.
   * 
   * \brief Once nodes and net devices are set into this object, no more set is
   * allowed. 
   */
  bool IsEmpty () const;
  /**
   * \brief Check if this is a BMSN topology
   * 
   * \returns True if the m_nodes and m_devices form a BMSN topology; 
   * False otherwise.
   *
   * To be a BMSN topology, several requirements must be met.
   * - Number of nodes must be a power of 2
   * - Number of links must be 2 times of the number of nodes
   * - Nodes are conceptually aligned on a N x N array, and
   * each node is identified by its coordinate (x, y) where
   * the range of x and y is [0, N)
   * - A net device on a (x, y)-node must has the IP address
   * "w.x.y.z". Where x and y are the coordinate of the node, z
   * is a number in the range [1, 4], and w can be any number 
   * in the range [1, 127] in order to compile with the Class-A
   * IP address format.
   * - A north facing net device must have an IP address "w.x.y.1"
   * and is connecting to a net device with IP "w.x.(y-1).3",
   * east facing must be "w.x.y.2" and is connecting "w.(x+1).y.4", 
   * south facing must be "w.x.y.3" and is connecting "w.x.(y+1).1", and
   * west facing must be "w.x.y.4" and is connecting "w.(x-1).y.2".
   * Round robin addition/substruction is used.
   *
   * \see BmsnGenerator
   */
  bool IsBmsnTopology () const;
  /**
   * \brief Get the node position for printing a Pajek topology file
   *
   * The method returns the node position by reading the interfaces'
   * IP address. See ::IsBmsnTopology() for the definition of node
   * position and IP address
   *
   * \see BaseTopology::GetNodePosition ()
   */
  string GetNodePosition (Ptr<Node> n) const;
};

} // namespace eecityu
} // namespace ns3

#endif //#define BMSN_TOPOLOGY_H
